package com.sangsang.ck.methods;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.methods.DeleteBatchByIds;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * ck语法的批量删除
 * 备注：区别于DeleteBatchByIds 这里不考虑逻辑删除的情况，直接物理删除
 *
 * @author liutangqi
 * @date 2023/12/26 10:07
 * @see DeleteBatchByIds 参考的这个
 */
public class CkBatchDeleteByIds extends AbstractMethod {

    private static final String METHOD_NAME = "ckBatchDeleteByIds";
    private static final String ORIGINAL_SQL = "<script>\nALTER TABLE  %s DELETE WHERE %s IN (%s)\n</script>";

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql = String.format(ORIGINAL_SQL, tableInfo.getTableName(), tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach("#{item}", "coll", (String) null, "item", ","));
        SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, Object.class);
        return this.addDeleteMappedStatement(mapperClass, METHOD_NAME, sqlSource);
    }
}
